package com.taxi.driver.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.taxi.driver.R;

/**
 * Created by Administrator on 2017/12/6.
 */
public class SlideView extends LinearLayout implements View.OnTouchListener {

    private static final int[] mAttr = {android.R.attr.text, android.R.attr.textSize};

    private float mTextSize;
    private String mContent;

    private FrameLayout mForeground;
    private TextView mTvContent;

    private float mDownX = 0;
    private SlideListener mSlideListener;

    public SlideView(Context context) {
        this(context, null);
    }

    public SlideView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    @SuppressWarnings("ResourceType")
    public SlideView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        TypedArray ta = context.obtainStyledAttributes(attrs, mAttr);
        mContent = ta.getString(0);
        mTextSize = ta.getDimension(1, 0);
        ta.recycle();

        initView(context);
    }

    private void initView(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.view_slide, this, false);
        addView(view);

        mForeground = (FrameLayout) view.findViewById(R.id.foreground);
        mTvContent = (TextView) view.findViewById(R.id.tv_content);

        if (!TextUtils.isEmpty(mContent)) {
            mTvContent.setText(mContent);
        }
        if (mTextSize > 0) {
            mTvContent.setTextSize(mTextSize);
        }

        mForeground.setOnTouchListener(this);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (mForeground.getBackground().getBounds().contains((int) event.getX(), (int) event.getY())) {
                    getParent().requestDisallowInterceptTouchEvent(true);
                    mDownX = event.getRawX();
                } else {
                    return false;
                }
                break;
            case MotionEvent.ACTION_MOVE:
                getParent().requestDisallowInterceptTouchEvent(true);
                float offset = event.getRawX() - mDownX;
                move(offset);
                break;
            case MotionEvent.ACTION_UP:
                getParent().requestDisallowInterceptTouchEvent(false);
                float upOffset = event.getRawX() - mDownX;
                up(upOffset);
                break;
        }
        return true;
    }

    private void move(float offset) {
        if (offset + mForeground.getLeft() >= 0) {
            mForeground.setTranslationX(offset);
        } else {
            mForeground.setTranslationX(0);
        }
    }

    private void up(float offset) {
        if (offset >= getWidth() * 0.7) {
            mForeground.setTranslationX(getWidth());
            if (mSlideListener != null) {
                mSlideListener.onSlideOver();
            }
        } else {
            mForeground.setTranslationX(0);
        }
    }

    public void setOnSlideListener(SlideListener slideListener) {
        mSlideListener = slideListener;
    }

    public interface SlideListener {
        void onSlideOver();
    }

    public void setContent(String content) {
        requestLayout();
        mContent = content;
        mTvContent.setText(content);
    }

    public void resetView() {
        requestLayout();
        mForeground.setTranslationX(0);
    }
}
